<template>
  <div class="login-container">
    <img class="back_img" :src="url" alt>
    <div class="login-box">
      <div class="title">
        <!-- <img src="../assets/img/logo.png" alt> -->
        气象信息服务平台
      </div>

      <div class="user_login">
        <span>用户登录</span>
        <span>UserLogin</span>
      </div>

      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <i slot="prefix" class="iconfont icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input show-password type="password" v-model="loginForm.password">
            <i slot="prefix" class="iconfont icon-3702mima"></i>
          </el-input>
        </el-form-item>
        <el-row>
          <el-col>
            <span class="rem_me" @click="lock = !lock">
              <!-- <i :class="{ali_iconfont:true,ali_iconduigou:true,lock_y_n:lock?true:false}"></i> -->
              <!-- <span :class="{lock_y_n:lock?true:false}">记住我</span> -->
            </span>
            <el-button class="determined" type="primary" @click="Submit_application">登录</el-button>
            <!-- <el-button type="info">重置</el-button> -->
          </el-col>
        </el-row>
      </el-form>

      <div class="footer">欢迎登录气象系统综合服务平台</div>
    </div>
  </div>
</template>

<script>
import Qs from "qs";

export default {
  // created() {
  //   this.log_next();
  // },
  mounted() {
    this.getCookie();
    // var img_url = window.localStorage.getItem("back_img");
    //

    // if (img_url) {
    //   this.url = img_url;
    //
    // }
  },
  data() {
    return {
      // 用户登录背景
      url: "https://www.raintec.cn/pic/upload/background/bg_login.png",
      // 是否记住我
      lock: false,

      // 用户登录表单数据对象(用户名、密码)
      loginForm: {
        username: "",
        password: ""
      },
      // e10adc3949ba59abbe56e057f20f883e
      // 给 各个表单域 定义校验规则
      loginFormRules: {
        username: [
          // required:非空  message:错误提示  trigger:触发校验机制
          { required: true, message: "请输入用户名称", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    // log_next() {

    // },

    async Submit_application() {
      //
      // let res = await this.$http.get(`node/getapp4`);
      // console.log(res);

      var _this = this;

      this.$refs.loginFormRef.validate(async valid => {
        if (valid) {
          // 如果记住我按钮被选中了 存入cookie
          // if (this.lock) {
          //
          //   _this.setCookie(
          //     _this.loginForm.username,
          //     _this.loginForm.password,
          //     7
          //   );
          // } else {
          //   // 清除cookie
          //
          //   _this.clearCookie();
          // }
          // 加密密码为32位
          // 发送登录请求
          // this.loginForm.password = this.$md5(this.loginForm.password);
          var u_obj = {};
          u_obj.username = this.loginForm.username;
          u_obj.password = this.$md5(this.loginForm.password);

          let res = await this.$http.post("/user/login", Qs.stringify(u_obj));
          // console.log(res);

          if (res.data.status != 0) {
            return this.$message.error("密码或用户名错误！");
          }

          if (res.data.data.type != 4)
            return this.$message.error("该用户不存在");
          this.$message.success("登录成功");
          // 存入token以通过验证
          window.sessionStorage.setItem("token", res.data.token);

          // 存入当前的用户的个人信息
          window.sessionStorage.setItem("user_info", JSON.stringify(res.data));
          window.sessionStorage.setItem(
            "user_pwd",
            JSON.stringify(this.loginForm)
          );
          this.$store.commit("set_user_info", JSON.stringify(res.data));

          // 编程式导航跳转到主页面
          this.$router.push("/home");
          u_obj = null;
          //
        }
      });
    },
    // setCookie(c_name, c_pwd, exdays) {
    //   var exdate = new Date(); //获取时间
    //   exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
    //   window.document.cookie =
    //     "username" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
    //   window.document.cookie =
    //     "password" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
    // },
    // 点击记住我
    getCookie() {
      //   if (document.cookie.length > 0) {
      //     var arr = document.cookie.split("; "); //这里显示的格式需要切割一下自己可输出看下
      //
      //     for (var i = 0; i < arr.length; i++) {
      //       var arr2 = arr[i].split("="); //再次切割
      //       if (arr2[0].trim() == "username") {
      //         this.loginForm.username = arr2[1]; //保存到保存数据的地方
      //       } else if (arr2[0].trim() == "password") {
      //         this.loginForm.password = arr2[1];
      //       }
      //     }
      //   }
    },
    // 清除记录
    clearCookie() {
      this.setCookie("", "", -1);
    }
  }
};
</script>

<style lang="less" scoped>
.back_img {
  width: 100%;
  height: 100%;
}

.lock_y_n {
  color: #007ee5;
}
.footer {
  font-size: 14px;
  color: #007ee5;
  text-align: center;
  line-height: 58px;
  height: 58px;
  position: absolute;
  bottom: 0px;
  width: 100%;
  background-color: #e1e4ed;
  border-radius: 0px 0px 10px 10px;
}
.el-input--prefix .el-input__inner {
  padding-left: 40px !important;
}
.login-container {
  // background-color: #05bdf9;
  // background-image: url(../assets/img/login_back.jpg);
  height: 100%;
  overflow: hidden;
  .login-box {
    width: 450px;
    height: 344px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0px 5px 5px 5px rgba(8, 8, 8, 0.2);
    .el-form {
      position: absolute;
      //   bottom: 0;
      top: 70px;
      width: 100%;
      padding: 20px;
      box-sizing: border-box;
      margin-bottom: 30px;

      .iconfont {
        padding-left: 15px;
      }
      .determined {
        width: 267px;
        float: right;
        background-color: #007ee5;
        font-size: 16px;
      }
      .rem_me {
        // 禁止用户选择文字
        user-select: none;
        color: #ccc;
        font-size: 16px;
        float: left;
        cursor: pointer;
        line-height: 40px;
        i {
          font-size: 20px;
        }
      }
    }
    .title {
      font-size: 40px;
      color: #fff;
      transform: translateY(-200%);
      text-align: center;
    }
    .user_login {
      margin-top: -20px;
      color: #429ced;
      display: flex;
      justify-content: space-between;
      padding: 0px 20px;
      :first-child {
        font-size: 26px;
      }
    }
  }
}
</style>